package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IFriendRemark;
import com.wbao.dianniu.listener.IFriendRemarkListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

public class FriendRemarkManager implements IFriendRemark {

	private IFriendRemarkListener mListener;
	private Context mContext;
	
	public FriendRemarkManager(Context context) {
		mContext = context;
	}
	
	@Override
	public void friendRemark(int accountId, int friendId, String remark) {
		StringEntity stringEntity = null;
		try {
			Batch batch = new Batch();
			batch.setToken(GlobalContext.getCacheToken());
			Command command = new Command();
			command.addAction(Action.ACTION_ACCOUNT_FRIEND_REMARK)
					.addArgs("accountId", accountId)
					.addArgs("friendId", friendId)
					.addArgs("remark", remark);
			batch.addCommand(command);
			stringEntity = new StringEntity(batch.toJson(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		HttpUtil http = new HttpUtil();
		http.req(mContext, stringEntity, new MyResponse() {
			
			@Override
			public void responseSucess(Object obj) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onFriendRemarkSuccess();
			}
			
			@Override
			public void responseFailure(int errorCode, String msg) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onFriendRemarkFailure(errorCode, msg);
			}
		});
	}

	@Override
	public boolean addFriendRemarkListener(IFriendRemarkListener listener) {
		mListener = listener;
		return mListener == null ? false : true;
	}

	@Override
	public boolean removeFriendRemarkListener(IFriendRemarkListener listener) {
		if (null == listener)
			return false;
		mListener = null;
		return true;
	}
	
}
